package ch.bfh.mobicomp.soda2.wifi.service;

import java.util.ArrayList;

import ch.bfh.mobicomp.soda2.wifi.utils.*;
import ch.bfh.mobicomp.soda2.wifi.utils.WifiApManager.ClientScanResult;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiConfiguration;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class WifiService extends IntentService {
	
	public static final String PREFS_NAME = WifiService.class.getName();
	private static final String TAG = WifiService.class.getName();
	
	public static final String ACTION_START = "ch.bfh.mobicomp.soda2.action.START";
	public static final String ACTION_STOP = "ch.bfh.mobicomp.soda2.action.STOP";
	public static final String ACTION_WIFI = "ch.bfh.mobicomp.soda2.action.WIFI";
	public static final String ACTION_AP = "ch.bfh.mobicomp.soda2.action.ACCESSPOINT";
	
	private WIFI_SERVICE_STATE state; 
	
	private WifiApManager wifiApManager;
	
	public WifiService() {
		super("");
	}
	
	public WifiService(String name) {
		super(name);
		
	}
	
	public static Intent getWifiServiceStartIntent() {
		return new Intent(ACTION_START);
	}
	
	public static Intent getRefreshClientListIntent() {
		Intent intent = new Intent(ACTION_AP);
		intent.putExtra("action", "refresh_clients");
		return intent;
	}
	
	public static Intent getWifiApOnOffIntent(Boolean switchOnOff) {
		Intent intent = new Intent(ACTION_AP);
		intent.putExtra("action", "toggle");
		intent.putExtra("enabled", switchOnOff);		
		return intent;
	}
	
	public static Intent getWifiDirectOnOffIntent() {
		
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		this.wifiApManager = new WifiApManager(this.getApplicationContext());
	}
	
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {		
		Log.d(TAG, "onStartCommand");
		
		final String intentAcction = intent.getAction();
		if(intentAcction.equals(ACTION_START)) {
			this.state = WIFI_SERVICE_STATE.STARTED;
		}
		else if(intentAcction.equals(ACTION_STOP)) {
			this.state = WIFI_SERVICE_STATE.STOPPED;
		}
		else if(intentAcction.equals(ACTION_WIFI)) {
					
		}
		else if(intentAcction.equals(ACTION_AP)) {
			String action = intent.getStringExtra("action");
			
			if(action.equals("toggle")) {
				Log.d(TAG, "Toggle AccessPoint");
				Boolean enabled = intent.getBooleanExtra("enabled", false);
				this.ToggleAccessPoint(enabled);
			}
			else if(action.equals("refresh_clients")) {
				//nothing to do yet...
			}
		}
		
		this.SavePreferences();
		
		return Service.START_STICKY;
	}
		
	@Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        
        this.SavePreferences();
    }
	
	public void ToggleAccessPoint(Boolean enabled) {
		WifiConfiguration config = this.getSoda2WifiConfiguration();
		
		if(enabled)
			this.wifiApManager.setWifiApEnabled(config, true);
		else
			this.wifiApManager.setWifiApEnabled(config, false);
	}
	
	public Boolean isApEnabled() {
		return this.wifiApManager.getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED
				|| this.wifiApManager.getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLING;
	}
	
	private void SavePreferences() {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		
		//service settings
		editor.putString("SERVICE_STATE", this.state.toString());
				
		//access point
		WifiConfiguration apConfig = this.getSoda2WifiConfiguration();
		editor.putBoolean("AP_ENABLED", this.isApEnabled());
		editor.putString("AP_SSID", apConfig.SSID);
		editor.putBoolean("AP_HIDDENSSID", apConfig.hiddenSSID);
		
		//
		String clients = "";
		ArrayList<ClientScanResult> list = this.wifiApManager.getClientList(true);
		for(ClientScanResult result : list) {
			clients += result.getHWAddr() + ";";
		}
		
		editor.putString("AP_CLIENTS", clients);
		
		editor.commit();
	}
	
	private WifiConfiguration getSoda2WifiConfiguration() {
		WifiConfiguration config = new WifiConfiguration();
		
		config.SSID = "SODA2";
		config.hiddenSSID = false;
		
		return config;
	}
	
	public WifiConfiguration getApConfiguration() {
		return this.wifiApManager.getWifiApConfiguration();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		
	}
}